
package com.probiz.estore.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.Constants;
import com.probiz.estore.webapp.util.RequestUtil;

public class PreLoginFrontFilter extends HttpServlet implements Filter {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -448278954862771679L;

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//记录登录前的url
		rememberLastUrl(request);
		chain.doFilter(request, response);

	}

	private void rememberLastUrl(ServletRequest request) {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String continueUrl = RequestUtil.getParameterNullSafe(httpRequest, Constants.SAVED_URL_KEY.toString());
		if(StringUtils.isNotBlank(continueUrl)&&!continueUrl.contains("signup")){
			//start:构造savedRequest对象，使得登录后能跳转到之前访问的页面
			httpRequest.getSession().setAttribute(Constants.SAVED_REQUEST_KEY, continueUrl);
			//end:构造savedRequest对象，使得登录后能跳转到之前访问的页面
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

}
